
package com.njrsrc.android.app;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.MKGeneralListener;
import com.baidu.mapapi.map.MKEvent;
import com.njrsrc.R;
import com.njrsrc.android.bean.AreaInfo;
import com.njrsrc.android.bean.IndustryInfo;
import com.njrsrc.android.bean.PositionInfo;
import com.njrsrc.android.db.helper.DBHelperImpl;
import com.njrsrc.common.Setting;
import com.njrsrc.common.parser.json.AreaParser;
import com.njrsrc.common.parser.json.CityInfo;
import com.njrsrc.common.parser.json.ProvinceInfo;
import com.njrsrc.common.parser.json.ZoneInfo;
import com.njrsrc.common.parser.sax.MySaxParser;

/**
 * 初始化配置工具和数据库工具
 * 
 * @author Oliverzhu 2013/4/14
 */
public class AppApplication extends Application {
    private static final String TAG = "AppApplication";

    private static AppApplication mInstance = null;

    public static SharedPreferences mPrefs;

    public static DBHelperImpl dbHelper;

    /** 用户ID */
    public static String mUserID;

    /** 用户令牌 */
    public static String mToken;

    /** 简历ID */
    public static String mResumeID;

    /** 是否已登录 */
    public static boolean isLogin;

    /** 登录用户名 */
    public static String mUserName;

    /** 头像地址 */
    public static String mHeadUrl;

    /** 人士来信count */
    public static String hrLetterCount;

    /** 行业列表 */
    public static List<IndustryInfo> industryList;

    /** 地区列表 */
    public static List<ProvinceInfo> areaList;

    /** 顶部地区列表 */
    public static List<ProvinceInfo> areaTopicList;

    /** 职位列表 */
    public static List<ProvinceInfo> positionList;

    /** 请选择地区 */
    public static List<AreaInfo> chooseArea = new ArrayList<AreaInfo>();
    /** 请选择行业 */
    public static List<IndustryInfo> chooseIndustry = new ArrayList<IndustryInfo>();
    /** 请选择职位 */
    public static List<PositionInfo> choosePosition = new ArrayList<PositionInfo>();

    public boolean m_bKeyRight = true;
    BMapManager mBMapManager = null;

    public static final String strKey = "7E98DADD7E14B6166F917942C902DD32CA91EA85";

    @Override
    public void onCreate() {
        super.onCreate();

        if (Setting.DEBUG)
            Log.i(TAG, "init Application");

        mInstance = this;

        // 获取配置文件工具
        mPrefs = PreferenceManager.getDefaultSharedPreferences(this);

        // 获取数据库工具
        dbHelper = new DBHelperImpl(this);

        // 获取行业列表
        industryList = getIndustryList();

        // 获取地区列表
        areaList = getProvinceList(R.raw.area);

        // 获取顶部地区列表
        areaTopicList = getProvinceList(R.raw.area_topic);

        // 获取职位列表
        positionList = getPositionList(R.raw.position);

        initEngineManager(this);
    }

    /** 获取行业列表 */
    public List<IndustryInfo> getIndustryList() {
        InputStream inputStream = getResources().openRawResource(R.raw.industry);
        MySaxParser parser = new MySaxParser();
        List<IndustryInfo> industryInfos = null;
        try {
            industryInfos = parser.getIndustryList(inputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return industryInfos;
    }

    /** 地区列表 */
    public List<ProvinceInfo> getProvinceList(int resID) {
        InputStream inputStream = getResources().openRawResource(resID);
        AreaParser parser = new AreaParser(inputStream);
        return parser.getProvinceList();
    }

    /** 职位列表 */
    public List<ProvinceInfo> getPositionList(int resID) {
        InputStream inputStream = getResources().openRawResource(resID);
        // 同样使用地区的解析方式
        AreaParser parser = new AreaParser(inputStream);
        return parser.getProvinceList();
    }

    public static AppApplication getInstance() {
        return mInstance;
    }

    @Override
    // 建议在您app的退出之前调用mapadpi的destroy()函数，避免重复初始化带来的时间消耗
    public void onTerminate() {
        if (mBMapManager != null) {
            mBMapManager.destroy();
            mBMapManager = null;
        }
        super.onTerminate();
    }

    public void initEngineManager(Context context) {
        if (mBMapManager == null) {
            mBMapManager = new BMapManager(context);
        }

        if (!mBMapManager.init(strKey, new MyGeneralListener())) {
            Toast.makeText(AppApplication.getInstance().getApplicationContext(),
                    "BMapManager  初始化错误!", Toast.LENGTH_LONG).show();
        }
    }

    // 常用事件监听，用来处理通常的网络错误，授权验证错误等
    static class MyGeneralListener implements MKGeneralListener {

        @Override
        public void onGetNetworkState(int iError) {
            if (iError == MKEvent.ERROR_NETWORK_CONNECT) {
                Toast.makeText(AppApplication.getInstance().getApplicationContext(), "您的网络出错啦！",
                        Toast.LENGTH_LONG).show();
            }
            else if (iError == MKEvent.ERROR_NETWORK_DATA) {
                Toast.makeText(AppApplication.getInstance().getApplicationContext(), "输入正确的检索条件！",
                        Toast.LENGTH_LONG).show();
            }
            // ...
        }

        @Override
        public void onGetPermissionState(int iError) {
            if (iError == MKEvent.ERROR_PERMISSION_DENIED) {
                // 授权Key错误：
                Toast.makeText(AppApplication.getInstance().getApplicationContext(),
                        "请在 DemoApplication.java文件输入正确的授权Key！", Toast.LENGTH_LONG).show();
                AppApplication.getInstance().m_bKeyRight = false;
            }
        }
    }

    /**
     * 退出时进行资源的释放
     */
    public static void release() {
        mUserID = null;
        mToken = null;
        mResumeID = null;
        isLogin = false;
        mUserName = null;
        mHeadUrl = null;
        hrLetterCount = null;
    }

    /**
     * 选择条件清空
     */
    public static void clear() {
        chooseArea.clear();
        chooseIndustry.clear();
        choosePosition.clear();
        for (int i = 0; i < areaList.size(); i++) {
        	ProvinceInfo p = areaList.get(i);
            p.setSelected(false);
            List<CityInfo> cities = p.getCityList();
            if(cities != null && cities.size() != 0)
            {
            	for(int j = 0;j < cities.size();j++)
            	{
            		CityInfo c = cities.get(j);
            		c.setSelected(false);
            		List<ZoneInfo> zones = c.getZoneList();
            		if(zones != null && zones.size() != 0)
            		{
            			for(int k = 0;k < zones.size();k++)
            			{
            				ZoneInfo z = zones.get(k);
            				z.setSelected(false);
            			}
            		}
            	}
            }
        }
        for (int i = 0; i < areaTopicList.size(); i++) {
        	ProvinceInfo p = areaTopicList.get(i);
            p.setSelected(false);
            List<CityInfo> cities = p.getCityList();
            if(cities != null && cities.size() != 0)
            {
            	for(int j = 0;j < cities.size();j++)
            	{
            		CityInfo c = cities.get(j);
            		c.setSelected(false);
            		List<ZoneInfo> zones = c.getZoneList();
            		if(zones != null && zones.size() != 0)
            		{
            			for(int k = 0;k < zones.size();k++)
            			{
            				ZoneInfo z = zones.get(k);
            				z.setSelected(false);
            			}
            		}
            	}
            }
        }
        for (int i = 0; i < industryList.size(); i++) {
            industryList.get(i).setSelected(false);
        }
        for (int i = 0; i < positionList.size(); i++) {
        	ProvinceInfo p = positionList.get(i);
            p.setSelected(false);
            List<CityInfo> cities = p.getCityList();
            if(cities != null && cities.size() != 0)
            {
            	for(int j = 0;j < cities.size();j++)
            	{
            		CityInfo c = cities.get(j);
            		c.setSelected(false);
            		List<ZoneInfo> zones = c.getZoneList();
            		if(zones != null && zones.size() != 0)
            		{
            			for(int k = 0;k < zones.size();k++)
            			{
            				ZoneInfo z = zones.get(k);
            				z.setSelected(false);
            			}
            		}
            	}
            }
        }
    }
}
